package game.Utils;

import game.SceneObject;

public class DestroyWatchdog extends SceneObject {

    protected SceneObject objToDestroy;
    protected long destroyTime;

    /**
     * Destroys and object after a time has passed.
     * @param objToDestroy Object to be destroyed
     * @param time Time to wait before destroying
     * @throws Exception
     */
    public DestroyWatchdog(SceneObject objToDestroy, double time) throws Exception {
        this.objToDestroy = objToDestroy;
        destroyTime = System.currentTimeMillis() + (int)(time*1000);
    }

    @Override
    public void update(double deltaTime) {
        if(System.currentTimeMillis() >= destroyTime){
            if(objToDestroy != null){
                objToDestroy.destroy();
            }
            this.destroy();
        }
    }
}
